package com.iwhalecloud.bss.kite.cucc.service.util.webService;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.xml.soap.SOAPException;

import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttachmentDTO;
import org.apache.axis.message.SOAPEnvelope;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ContractRequestDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ContractSimpleDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.AccessoryInfoRsp;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.CustContractRsp;


/**
 * @Description: TODO
 * @author lyx
 * @date 2020年11月12日
 * @version V1.0
 * @Package com.iwhalecloud.bss.kite.cucc.service.util.webService
 */
public class WebServiceHelper {

    public static String replaceHeadReq(String reqString, String provinceCode, String mediaInfo, String serviceName, String operateName) {

        Date d = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");

        reqString = KiteStringUtils.replace(reqString, "${ORIG_DOMAIN}", "BBSS");
        reqString = KiteStringUtils.replace(reqString, "${SERVICE_NAME}", serviceName);
        reqString = KiteStringUtils.replace(reqString, "${OPERATE_NAME}", operateName);
        reqString = KiteStringUtils.replace(reqString, "${ACTION_CODE}", "0");
        reqString = KiteStringUtils.replace(reqString, "${ACTION_RELATION}", "1");
        reqString = KiteStringUtils.replace(reqString, "${ROUTE_TYPE}", "04");
        reqString = KiteStringUtils.replace(reqString, "${ROUTE_VALUE}", "1");
        reqString = KiteStringUtils.replace(reqString, "${PROC_ID}", String.valueOf(d.getTime()) + new Random().nextInt(99999));
        reqString = KiteStringUtils.replace(reqString, "${TRANS_IDO}", String.valueOf(d.getTime()) + new Random().nextInt(99999));
        reqString = KiteStringUtils.replace(reqString, "${PROCESS_TIME}", sdf1.format(d));
        reqString = KiteStringUtils.replace(reqString, "${PROVINCE_CODE}", provinceCode);
        reqString = KiteStringUtils.replace(reqString, "${VERSION}", "1.0.0");
        reqString = KiteStringUtils.replace(reqString, "${CUTOFFDAY}", sdf2.format(d));
        reqString = KiteStringUtils.replace(reqString, "${OSNDUNS}", "0013");
        reqString = KiteStringUtils.replace(reqString, "${TEST_FLAG}", "1");

        if (KiteStringUtils.isBlank(mediaInfo)) {
            reqString = KiteStringUtils.replace(reqString, "<unib2:MEDIA_INFO>${MEDIA_INFO}</unib2:MEDIA_INFO>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${MEDIA_INFO}", mediaInfo);
        }
        return reqString;

    }

    public static String replaceContractReq(String provinceCode, String contractNo, String custId, String contractId,
        String contractName, String dataCode, String mediaInfo) {

        String reqString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unib=\"http://ws.chinaunicom.cn/SAL_BUSA_QryContractForOrderSer/unibssBody\" xmlns:unib1=\"http://ws.chinaunicom.cn/unibssHead\" xmlns:qry=\"http://ws.chinaunicom.cn/SAL_BUSA_QryContractForOrderSer/unibssBody/qryContractForOrderReq\" xmlns:unib2=\"http://ws.chinaunicom.cn/unibssAttached\">\r\n" +
            "   <soapenv:Header/>\r\n" +
            "   <soapenv:Body>\r\n" +
            "      <unib:QRYCUSTCONTRACTINFO_INPUT>\r\n" +
            "         <unib1:UNI_BSS_HEAD>\r\n" +
            "            <unib1:ORIG_DOMAIN>${ORIG_DOMAIN}</unib1:ORIG_DOMAIN>\r\n" +
            "            <unib1:SERVICE_NAME>${SERVICE_NAME}</unib1:SERVICE_NAME>\r\n" +
            "            <unib1:OPERATE_NAME>${OPERATE_NAME}</unib1:OPERATE_NAME>\r\n" +
            "            <unib1:ACTION_CODE>${ACTION_CODE}</unib1:ACTION_CODE>\r\n" +
            "            <unib1:ACTION_RELATION>${ACTION_RELATION}</unib1:ACTION_RELATION>\r\n" +
            "            <!--1 to 2 repetitions:-->\r\n" +
            "            <unib1:ROUTING>\r\n" +
            "               <unib1:ROUTE_TYPE>${ROUTE_TYPE}</unib1:ROUTE_TYPE>\r\n" +
            "               <unib1:ROUTE_VALUE>${ROUTE_VALUE}</unib1:ROUTE_VALUE>\r\n" +
            "            </unib1:ROUTING>\r\n" +
            "            <unib1:PROC_ID>${PROC_ID}</unib1:PROC_ID>\r\n" +
            "            <unib1:TRANS_IDO>${TRANS_IDO}</unib1:TRANS_IDO>\r\n" +
            "            <!--Optional:-->\r\n" +
            "            <!-- <unib1:TRANS_IDH>${TRANS_IDH}</unib1:TRANS_IDH> -->\r\n" +
            "            <unib1:PROCESS_TIME>${PROCESS_TIME}</unib1:PROCESS_TIME>\r\n" +
            "            <unib1:BBSS_INFO>\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:OPER_ID>${OPER_ID}</unib1:OPER_ID> -->\r\n" +
            "               <unib1:PROVINCE_CODE>${PROVINCE_CODE}</unib1:PROVINCE_CODE>\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:EPARCHY_CODE>${EPARCHY_CODE}</unib1:EPARCHY_CODE> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:CITY_CODE>${CITY_CODE}</unib1:CITY_CODE> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:CHANNEL_ID>${CHANNEL_ID}</unib1:CHANNEL_ID> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:CHANNEL_TYPE>${CHANNEL_TYPE}</unib1:CHANNEL_TYPE> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:DATA_SERVICE_CODE>${DATA_SERVICE_CODE}</unib1:DATA_SERVICE_CODE> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:DATA_OPR_CODE>${DATA_OPR_CODE}</unib1:DATA_OPR_CODE> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:DATA_TRANS_ID>${DATA_TRANS_ID}</unib1:DATA_TRANS_ID> -->\r\n" +
            "               <unib1:VERSION>${VERSION}</unib1:VERSION>\r\n" +
            "            </unib1:BBSS_INFO>\r\n" +
            "            <unib1:SP_RESERVE>\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:TRANS_IDC>${TRANS_IDC}</unib1:TRANS_IDC> -->\r\n" +
            "               <unib1:CUTOFFDAY>${CUTOFFDAY}</unib1:CUTOFFDAY>\r\n" +
            "               <unib1:OSNDUNS>${OSNDUNS}</unib1:OSNDUNS>\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:HSNDUNS>${HSNDUNS}</unib1:HSNDUNS> -->\r\n" +
            "               <!--Optional:-->\r\n" +
            "               <!-- <unib1:CONV_ID>${CONV_ID}</unib1:CONV_ID> -->\r\n" +
            "            </unib1:SP_RESERVE>\r\n" +
            "            <unib1:TEST_FLAG>${TEST_FLAG}</unib1:TEST_FLAG>\r\n" +
            "            <!--Optional:-->\r\n" +
            "            <!-- <unib1:MSG_SENDER>${MSG_SENDER}</unib1:MSG_SENDER> -->\r\n" +
            "            <!--Optional:-->\r\n" +
            "            <!-- <unib1:MSG_RECEIVER>${MSG_RECEIVER}</unib1:MSG_RECEIVER> -->\r\n" +
            "         </unib1:UNI_BSS_HEAD>\r\n" +
            "         <unib:UNI_BSS_BODY>\r\n" +
            "            <qry:qryCustContractInfo_REQ>\r\n" +
            "               <qry:qryCustContractInfo_REQ_BODY>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <!-- <qry:STAFF_CODE>${STAFF_CODE}</qry:STAFF_CODE> -->\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <qry:CONTRACT_NO>${CONTRACT_NO}</qry:CONTRACT_NO>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <qry:CUST_ID>${CUST_ID}</qry:CUST_ID>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <qry:CONTRACT_ID>${CONTRACT_ID}</qry:CONTRACT_ID>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <qry:CONTRACT_NAME>${CONTRACT_NAME}</qry:CONTRACT_NAME>\r\n" +
            "                  <qry:PROVINCE_CODE>${PROVINCE_CODE}</qry:PROVINCE_CODE>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <qry:DATA_CODE>${DATA_CODE}</qry:DATA_CODE>\r\n" +
            "               </qry:qryCustContractInfo_REQ_BODY>\r\n" +
            "            </qry:qryCustContractInfo_REQ>\r\n" +
            "         </unib:UNI_BSS_BODY>\r\n" +
            "         <unib2:UNI_BSS_ATTACHED>\r\n" +
            "            <!--Optional:-->\r\n" +
            "            <unib2:MEDIA_INFO>${MEDIA_INFO}</unib2:MEDIA_INFO>\r\n" +
            "         </unib2:UNI_BSS_ATTACHED>\r\n" +
            "      </unib:QRYCUSTCONTRACTINFO_INPUT>\r\n" +
            "   </soapenv:Body>\r\n" +
            "</soapenv:Envelope>\r\n";

        reqString = replaceHeadReq(reqString, provinceCode, mediaInfo, "SAL_BUSA_QryContractForOrderSer", "qryContractForOrder");

        if (KiteStringUtils.isBlank(contractNo)) {
            reqString = KiteStringUtils.replace(reqString, "<qry:CONTRACT_NO>${CONTRACT_NO}</qry:CONTRACT_NO>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${CONTRACT_NO}", contractNo);
        }

        if (KiteStringUtils.isBlank(custId)) {
            reqString = KiteStringUtils.replace(reqString, "<qry:CUST_ID>${CUST_ID}</qry:CUST_ID>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${CUST_ID}", custId);
        }

        if (KiteStringUtils.isBlank(contractId)) {
            reqString = KiteStringUtils.replace(reqString, "<qry:CONTRACT_ID>${CONTRACT_ID}</qry:CONTRACT_ID>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${CONTRACT_ID}", contractId);
        }

        if (KiteStringUtils.isBlank(contractName)) {
            reqString = KiteStringUtils.replace(reqString, "<qry:CONTRACT_NAME>${CONTRACT_NAME}</qry:CONTRACT_NAME>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${CONTRACT_NAME}", contractName);
        }

        if (KiteStringUtils.isBlank(dataCode)) {
            reqString = KiteStringUtils.replace(reqString, "<qry:DATA_CODE>${DATA_CODE}</qry:DATA_CODE>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${DATA_CODE}", dataCode);
        }
        return reqString;
    }

    public static List<CustContractRsp> dealContractResp(SOAPEnvelope rsp) throws SOAPException {
        List<CustContractRsp> list = new ArrayList<>();
        NodeList nodeList = rsp.getBody().getLastChild().getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (KiteStringUtils.equals(node.getNodeName(), "UNI_BSS_BODY")) {
                NodeList rspBodyList = node.getLastChild().getLastChild().getLastChild().getChildNodes();
                for (int j = 0; j < rspBodyList.getLength(); j++) {
                    NodeList nodeContractList = rspBodyList.item(j).getChildNodes();
                    CustContractRsp custContractRsp = new CustContractRsp();
                    for (int k = 0; k < nodeContractList.getLength(); k++) {
                        Node nodeContract = nodeContractList.item(k);
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_ID")) {
                            custContractRsp.setContractId(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_NO")) {
                            custContractRsp.setContractNo(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_NAME")) {
                            custContractRsp.setContractName(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "SIGN_DATE")) {
                            custContractRsp.setSignDate(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "START_DATE")) {
                            custContractRsp.setStartDate(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "END_DATE")) {
                            custContractRsp.setEndDate(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "OPER_NO")) {
                            custContractRsp.setOperNo(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "OPER_NAME")) {
                            custContractRsp.setOperName(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "OPER_DATE")) {
                            custContractRsp.setOperDate(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "ACCESSORY_ID")) {
                         /*   custContractRsp.setAccessoryId(nodeContract.getFirstChild().toString());*/
                            List<KitePosAttachmentDTO> ATTACHMENTS=new ArrayList<KitePosAttachmentDTO>();
                            String ACCESSORY_ID = KiteStringUtils.substring(nodeContract.getFirstChild().toString(), 0,
                                KiteStringUtils.lastIndexOf(nodeContract.getFirstChild().toString(), "*_*"));
                            String ACCESSORY_NAME = KiteStringUtils.substring(nodeContract.getFirstChild().toString(),
                                KiteStringUtils.lastIndexOf(nodeContract.getFirstChild().toString(), "*_*")+3);
                            String[] ac = ACCESSORY_ID.split("\\|");
                            String[] an = ACCESSORY_NAME.split("\\|");
                            for (int s = 0; s < ac.length; s++) {
                                KitePosAttachmentDTO ATTACHMENT=new KitePosAttachmentDTO();
                                // url包含"*_*"的直接取上面切分好的名称,否则说明url是用"|"分隔的多个附件,那么按照切分的url动态解析
                                ATTACHMENT.setUrl(ac[s]);
                                ATTACHMENT.setName(an[s]);
                                ATTACHMENTS.add(ATTACHMENT);
                            }
                            custContractRsp.setAttachments(ATTACHMENTS);
                        }

                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "DATA_CODE")) {
                            custContractRsp.setDataCode(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "STATUS_CODE")) {
                            custContractRsp.setStatusCode(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_SUM")) {
                            custContractRsp.setContractSum(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_CURRENCY")) {
                            custContractRsp.setContractCurrency(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "COMPANY_NAME")) {
                            custContractRsp.setCompanyName(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_TYPE")) {
                            custContractRsp.setContractType(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_SHAPE")) {
                            custContractRsp.setContractShape(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_DRAFTERS")) {
                            custContractRsp.setContractDrafters(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_SCOPE")) {
                            custContractRsp.setContractScope(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "CONTRACT_EXECUTOR")) {
                            custContractRsp.setContractExecutor(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "HR_ORG_NAME")) {
                            custContractRsp.setHrOrgName(nodeContract.getFirstChild().toString());
                        }
                        if (KiteStringUtils.equals(nodeContract.getNodeName(), "ACCESSORY_LIST")) {
                            NodeList accessoryNodeList = nodeContract.getChildNodes();
                            List<AccessoryInfoRsp> accessoryList = new ArrayList<>();
                            for (int l = 0; l < accessoryNodeList.getLength(); l++) {
                                NodeList accessoryInfoList = accessoryNodeList.item(l).getChildNodes();
                                AccessoryInfoRsp accessory = new AccessoryInfoRsp();
                                for (int m = 0; m < accessoryInfoList.getLength(); m++) {
                                    Node accessoryInfo = accessoryInfoList.item(m);
                                    if (KiteStringUtils.equals(accessoryInfo.getNodeName(), "ORIGINAL_NAME")) {
                                        accessory.setOriginalName(accessoryInfo.getFirstChild().toString());
                                    }
                                    if (KiteStringUtils.equals(accessoryInfo.getNodeName(), "LOCATION_URL")) {
                                        accessory.setLocaltionUrl(accessoryInfo.getFirstChild().toString());
                                    }
                                }
                                accessoryList.add(accessory);
                            }
                            custContractRsp.setAccessoryInfo(accessoryList);
                        }
                    }
                    list.add(custContractRsp);
                }
            }
        }
        return list;
    }

    public static String replaceCreateContractReq(ContractRequestDTO contractRequestDTO) {

        String reqString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unib=\"http://ws.chinaunicom.cn/SAL_BUSA_CREATEFMTCONTRACTINFOSer/unibssBody\" xmlns:unib1=\"http://ws.chinaunicom.cn/unibssHead\" xmlns:cre=\"http://ws.chinaunicom.cn/SAL_BUSA_CREATEFMTCONTRACTINFOSer/unibssBody/CREATEFMTCONTRACTINFOSerReq\" xmlns:unib2=\"http://ws.chinaunicom.cn/unibssAttached\">\r\n" +
            "   <soapenv:Header/>\r\n" +
            "   <soapenv:Body>\r\n" +
            "      <unib:CREATEFMTCONTRACTINFO_INPUT>\r\n" +
            "         <unib1:UNI_BSS_HEAD>\r\n" +
            "            <unib1:ORIG_DOMAIN>${ORIG_DOMAIN}</unib1:ORIG_DOMAIN>\r\n" +
            "            <unib1:SERVICE_NAME>${SERVICE_NAME}</unib1:SERVICE_NAME>\r\n" +
            "            <unib1:OPERATE_NAME>${OPERATE_NAME}</unib1:OPERATE_NAME>\r\n" +
            "            <unib1:ACTION_CODE>${ACTION_CODE}</unib1:ACTION_CODE>\r\n" +
            "            <unib1:ACTION_RELATION>${ACTION_RELATION}</unib1:ACTION_RELATION>\r\n" +
            "            <!--1 to 2 repetitions:-->\r\n" +
            "            <unib1:ROUTING>\r\n" +
            "               <unib1:ROUTE_TYPE>${ROUTE_TYPE}</unib1:ROUTE_TYPE>\r\n" +
            "               <unib1:ROUTE_VALUE>${ROUTE_VALUE}</unib1:ROUTE_VALUE>\r\n" +
            "            </unib1:ROUTING>\r\n" +
            "            <unib1:PROC_ID>${PROC_ID}</unib1:PROC_ID>\r\n" +
            "            <unib1:TRANS_IDO>${TRANS_IDO}</unib1:TRANS_IDO>\r\n" +
            "            <unib1:PROCESS_TIME>${PROCESS_TIME}</unib1:PROCESS_TIME>\r\n" +
            "            <unib1:BBSS_INFO>\r\n" +
            "               <unib1:PROVINCE_CODE>${PROVINCE_CODE}</unib1:PROVINCE_CODE>\r\n" +
            "               <unib1:VERSION>${VERSION}</unib1:VERSION>\r\n" +
            "            </unib1:BBSS_INFO>\r\n" +
            "            <unib1:SP_RESERVE>\r\n" +
            "               <unib1:CUTOFFDAY>${CUTOFFDAY}</unib1:CUTOFFDAY>\r\n" +
            "               <unib1:OSNDUNS>${OSNDUNS}</unib1:OSNDUNS>\r\n" +
            "            </unib1:SP_RESERVE>\r\n" +
            "            <unib1:TEST_FLAG>${TEST_FLAG}</unib1:TEST_FLAG>\r\n" +
            "         </unib1:UNI_BSS_HEAD>\r\n" +
            "         <unib:UNI_BSS_BODY>\r\n" +
            "            <cre:CREATEFMTCONTRACTINFO_REQ>\r\n" +
            "               <cre:CREATEFMTCONTRACTINFO_REQ_BODY>\r\n" +
            "                  <cre:COMPANY_ID>${COMPANY_ID}</cre:COMPANY_ID>\r\n" +
            "                  <cre:COMPANY_NAME>${COMPANY_NAME}</cre:COMPANY_NAME>\r\n" +
            "                  <cre:CONTRAC_TNAME>${CONTRAC_TNAME}</cre:CONTRAC_TNAME>\r\n" +
            "                  <cre:CONTRACT_TYPE>${CONTRACT_TYPE}</cre:CONTRACT_TYPE>\r\n" +
            "                  <cre:CONTRACT_DRAFTERS>${CONTRACT_DRAFTERS}</cre:CONTRACT_DRAFTERS>\r\n" +
            "                  <cre:CONTRACT_EXECUTOR>${CONTRACT_EXECUTOR}</cre:CONTRACT_EXECUTOR>\r\n" +
            "                  <cre:IS_FIXEDMONEY>${IS_FIXEDMONEY}</cre:IS_FIXEDMONEY>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <cre:CONTRACT_SUM>${CONTRACT_SUM}</cre:CONTRACT_SUM>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <cre:CON_REC_NO>${CON_REC_NO}</cre:CON_REC_NO>\r\n" +
            "                  <cre:CONTRACT_CURRENCY>${CONTRACT_CURRENCY}</cre:CONTRACT_CURRENCY>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <cre:CONTRACT_NOTE>${CONTRACT_NOTE}</cre:CONTRACT_NOTE>\r\n" +
            "                  <cre:CUST_NAME>${CUST_NAME}</cre:CUST_NAME>\r\n" +
            "                  <cre:CUST_ID>${CUST_ID}</cre:CUST_ID>\r\n" +
            "                  <cre:CUST_CODE>${CUST_CODE}</cre:CUST_CODE>\r\n" +
            "                  <cre:CUST_TYPE>${CUST_TYPE}</cre:CUST_TYPE>\r\n" +
            "                  <cre:PROVINCE_CODE>${PROVINCE_CODE}</cre:PROVINCE_CODE>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <cre:CITY_CODE>${CITY_CODE}</cre:CITY_CODE>\r\n" +
            "                  <!--Optional:-->\r\n" +
            "                  <cre:COUNTY_CODE>${COUNTY_CODE}</cre:COUNTY_CODE>\r\n" +
            "                  <cre:DEPART_ID>${DEPART_ID}</cre:DEPART_ID>\r\n" +
            "                  <cre:DEPART_NAME>${DEPART_NAME}</cre:DEPART_NAME>\r\n" +
            "                  <cre:STATUS_CODE>${STATUS_CODE}</cre:STATUS_CODE>\r\n" +
            "                  <cre:ACCESSORY_ID>${ACCESSORY_ID}</cre:ACCESSORY_ID>\r\n" +
            "                  <cre:OPER_NO>${OPER_NO}</cre:OPER_NO>\r\n" +
            "                  <cre:OPER_NAME>${OPER_NAME}</cre:OPER_NAME>\r\n" +
            "               </cre:CREATEFMTCONTRACTINFO_REQ_BODY>\r\n" +
            "            </cre:CREATEFMTCONTRACTINFO_REQ>\r\n" +
            "         </unib:UNI_BSS_BODY>\r\n" +
            "         <unib2:UNI_BSS_ATTACHED>\r\n" +
            "            <!--Optional:-->\r\n" +
            "            <unib2:MEDIA_INFO>${MEDIA_INFO}</unib2:MEDIA_INFO>\r\n" +
            "         </unib2:UNI_BSS_ATTACHED>\r\n" +
            "      </unib:CREATEFMTCONTRACTINFO_INPUT>\r\n" +
            "   </soapenv:Body>\r\n" +
            "</soapenv:Envelope>";

//        Date d = new Date();
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");

//        reqString = KiteStringUtils.replace(reqString, "${ORIG_DOMAIN}", "BBSS");
//        reqString = KiteStringUtils.replace(reqString, "${SERVICE_NAME}", "SAL_BUSA_CREATEFMTCONTRACTINFOSer");
//        reqString = KiteStringUtils.replace(reqString, "${OPERATE_NAME}", "CREATEFMTCONTRACTINFOSer");
//        reqString = KiteStringUtils.replace(reqString, "${ACTION_CODE}", "0");
//        reqString = KiteStringUtils.replace(reqString, "${ACTION_RELATION}", "0");
//        reqString = KiteStringUtils.replace(reqString, "${ROUTE_TYPE}", "04");
//        reqString = KiteStringUtils.replace(reqString, "${ROUTE_VALUE}", "1");
//        reqString = KiteStringUtils.replace(reqString, "${PROC_ID}", String.valueOf(d.getTime()) + new Random().nextInt(99999));
//        reqString = KiteStringUtils.replace(reqString, "${TRANS_IDO}", String.valueOf(d.getTime()) + new Random().nextInt(99999));
//        reqString = KiteStringUtils.replace(reqString, "${PROCESS_TIME}", sdf1.format(d));
//        reqString = KiteStringUtils.replace(reqString, "${PROVINCE_CODE}", contractRequestDTO.getProvinceCode());
//        reqString = KiteStringUtils.replace(reqString, "${VERSION}", "1.0.0");
//        reqString = KiteStringUtils.replace(reqString, "${CUTOFFDAY}", sdf2.format(d));
//        reqString = KiteStringUtils.replace(reqString, "${OSNDUNS}", "0013");
//        reqString = KiteStringUtils.replace(reqString, "${TEST_FLAG}", "1");

        reqString = replaceHeadReq(reqString, contractRequestDTO.getProvinceCode(), null, "SAL_BUSA_CREATEFMTCONTRACTINFOSer", "CREATEFMTCONTRACTINFOSer");

        reqString = KiteStringUtils.replace(reqString, "<unib2:MEDIA_INFO>${MEDIA_INFO}</unib2:MEDIA_INFO>", "");

        reqString = KiteStringUtils.replace(reqString, "${COMPANY_ID}", contractRequestDTO.getCompanyId());
        reqString = KiteStringUtils.replace(reqString, "${COMPANY_NAME}", contractRequestDTO.getCompanyName());
        reqString = KiteStringUtils.replace(reqString, "${CONTRAC_TNAME}", contractRequestDTO.getContractName());
        reqString = KiteStringUtils.replace(reqString, "${CONTRACT_TYPE}", contractRequestDTO.getContractType());
        reqString = KiteStringUtils.replace(reqString, "${CONTRACT_DRAFTERS}", contractRequestDTO.getContractDrafters());
        reqString = KiteStringUtils.replace(reqString, "${CONTRACT_EXECUTOR}", contractRequestDTO.getContractExecutor());
        reqString = KiteStringUtils.replace(reqString, "${IS_FIXEDMONEY}", contractRequestDTO.getIsFixedMoney());

        if (KiteStringUtils.isNotBlank(contractRequestDTO.getContractSum())) {
            reqString = KiteStringUtils.replace(reqString, "${CONTRACT_SUM}", contractRequestDTO.getContractSum());
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "<cre:CONTRACT_SUM>${CONTRACT_SUM}</cre:CONTRACT_SUM>", "");
        }

        if (KiteStringUtils.isNotBlank(contractRequestDTO.getConRecNo())) {
            reqString = KiteStringUtils.replace(reqString, "${CON_REC_NO}", contractRequestDTO.getConRecNo());
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "<cre:CON_REC_NO>${CON_REC_NO}</cre:CON_REC_NO>", "");
        }

        reqString = KiteStringUtils.replace(reqString, "${CONTRACT_CURRENCY}", contractRequestDTO.getContractCurrency());

        if (KiteStringUtils.isNotBlank(contractRequestDTO.getContractNote())) {
            reqString = KiteStringUtils.replace(reqString, "${CONTRACT_NOTE}", contractRequestDTO.getContractNote());
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "<cre:CONTRACT_NOTE>${CONTRACT_NOTE}</cre:CONTRACT_NOTE>", "");
        }

        reqString = KiteStringUtils.replace(reqString, "${CUST_NAME}", contractRequestDTO.getCustName());
        reqString = KiteStringUtils.replace(reqString, "${CUST_ID}", contractRequestDTO.getCustId());
        reqString = KiteStringUtils.replace(reqString, "${CUST_CODE}", contractRequestDTO.getCustCode());
        reqString = KiteStringUtils.replace(reqString, "${CUST_TYPE}", contractRequestDTO.getCustType());
        reqString = KiteStringUtils.replace(reqString, "${PROVINCE_CODE}", contractRequestDTO.getProvinceCode());

        if (KiteStringUtils.isBlank(contractRequestDTO.getCityCode())) {
            reqString = KiteStringUtils.replace(reqString, "<cre:CITY_CODE>${CITY_CODE}</cre:CITY_CODE>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${CITY_CODE}", contractRequestDTO.getCityCode());
        }

        if (KiteStringUtils.isBlank(contractRequestDTO.getCountyCode())) {
            reqString = KiteStringUtils.replace(reqString, "<cre:COUNTY_CODE>${COUNTY_CODE}</cre:COUNTY_CODE>", "");
        }
        else {
            reqString = KiteStringUtils.replace(reqString, "${COUNTY_CODE}", contractRequestDTO.getCountyCode());
        }

        reqString = KiteStringUtils.replace(reqString, "${DEPART_ID}", contractRequestDTO.getDepartId());
        reqString = KiteStringUtils.replace(reqString, "${DEPART_NAME}", contractRequestDTO.getDepartName());
        reqString = KiteStringUtils.replace(reqString, "${STATUS_CODE}", contractRequestDTO.getStatusCode());
        reqString = KiteStringUtils.replace(reqString, "${ACCESSORY_ID}", contractRequestDTO.getAccessoryId());
        reqString = KiteStringUtils.replace(reqString, "${OPER_NO}", contractRequestDTO.getOperNo());
        reqString = KiteStringUtils.replace(reqString, "${OPER_NAME}", contractRequestDTO.getOperName());

        return reqString;
    }

    public static ContractSimpleDTO dealCreateContractResp(SOAPEnvelope rsp,String accessory_id) throws SOAPException {
        ContractSimpleDTO contractSimpleDTO = new ContractSimpleDTO();
        NodeList nodeList = rsp.getLastChild().getFirstChild().getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (KiteStringUtils.equals(node.getNodeName(), "UNI_BSS_BODY")) {
                NodeList rspList = node.getFirstChild().getChildNodes();
                if (!KiteStringUtils.isEqual(getNodeByName("RSP_CODE", rspList).getFirstChild().toString(), "0000")) {
                    ErrorConsts.INF_INVOKE_EXCEPTION.throwOut(getNodeByName("RSP_DESC", nodeList).getFirstChild().toString());
                }
                Node rspBody = getNodeByName("CREATEFMTCONTRACTINFO_RSP_BODY", rspList);
                for (int j = 0; j < rspBody.getChildNodes().getLength(); j++) {
                    Node createRsp = rspBody.getChildNodes().item(j);
                    if (KiteStringUtils.equals(createRsp.getNodeName(), "CONTRACT_NO")) {
                        contractSimpleDTO.setContractNo(createRsp.getFirstChild().toString());
                    }
                    if (KiteStringUtils.equals(createRsp.getNodeName(), "CONTRACT_NAME")) {
                        contractSimpleDTO.setContractName(createRsp.getFirstChild().toString());
                    }
                    if (KiteStringUtils.equals(createRsp.getNodeName(), "CONTRACT_ID")) {
                        contractSimpleDTO.setContractId(createRsp.getFirstChild().toString());
                    }
                }

                break;
            }
        }
        if (KiteStringUtils.isNotEmpty(accessory_id)) {
            /*   custContractRsp.setAccessoryId(nodeContract.getFirstChild().toString());*/
            List<KitePosAttachmentDTO> ATTACHMENTS=new ArrayList<KitePosAttachmentDTO>();
            String ACCESSORY_ID = KiteStringUtils.substring(accessory_id, 0,
                KiteStringUtils.lastIndexOf(accessory_id, "*_*"));
            String ACCESSORY_NAME = KiteStringUtils.substring(accessory_id,
                KiteStringUtils.lastIndexOf(accessory_id, "*_*")+3);
            String[] ac = ACCESSORY_ID.split("\\|");
            String[] an = ACCESSORY_NAME.split("\\|");
            for (int s = 0; s < ac.length; s++) {
                KitePosAttachmentDTO ATTACHMENT=new KitePosAttachmentDTO();
                // url包含"*_*"的直接取上面切分好的名称,否则说明url是用"|"分隔的多个附件,那么按照切分的url动态解析
                ATTACHMENT.setUrl(ac[s]);
                ATTACHMENT.setName(an[s]);
                ATTACHMENTS.add(ATTACHMENT);
            }
            contractSimpleDTO.setAttachments(ATTACHMENTS);
        }
        return contractSimpleDTO;
    }

    public static Node getNodeByName(String nodeName, NodeList nodeList) {
        if (Objects.isNull(nodeList) || nodeList.getLength() == 0) {
            return null;
        }
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (KiteStringUtils.isEqual(node.getNodeName(), nodeName)) {
                return node;
            }
        }
        return null;
    }
}
